package cn.daniellee.plugin;

import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class AntiModifySpawner extends JavaPlugin {

	private FileConfiguration config;

	private static String prefix;

	private static String[] allTypeArray;

	public void onEnable(){
		loadConfig();
		getLogger().info(" ");
		getLogger().info(">>>>>>>>>>>>>>>>>>>>>>>> AntiModifySpawner Loaded <<<<<<<<<<<<<<<<<<<<<<<<");
		getLogger().info(">>>>> If you encounter any bugs, please contact author's QQ: 768318841 <<<<<");
		getLogger().info(" ");

		//加载全部类型为字符串数组
		EggType[] eggTypes =  EggType.values();
		allTypeArray = new String[eggTypes.length];
		for (int i = 0; i < eggTypes.length; i++) {
			allTypeArray[i] = eggTypes[i].toString();
		}

		Bukkit.getPluginManager().registerEvents(new Listener() {
			@EventHandler
			public void onPlayerInteract(PlayerInteractEvent e) {
				if(e.getPlayer().isOp()) return;
				if(e.isCancelled()) return;
				if(!e.hasItem() || !e.hasBlock()) return;
				String itemType = e.getItem().getType().toString();
				if(e.getClickedBlock().getType().equals(Material.SPAWNER) && itemType.endsWith("_SPAWN_EGG")) {
					List<String> currentList = config.getStringList("allow-type");
					if (!currentList.contains(itemType.replace("_SPAWN_EGG", ""))) {
						e.getPlayer().sendMessage((prefix + config.get("prohibit-use")).replace("&", "§"));
						e.setCancelled(true);
					}
				}
			}
		}, this);
		Bukkit.getPluginCommand("acs").setExecutor(this);
	}

	private void loadConfig() {
		getLogger().info("[AntiModifySpawner] Loading config...");
		config = getConfig();
		prefix = "&7[&b" + config.get("prompt-prefix") + "&7] &3: &r";
		if(!getDataFolder().exists()) getDataFolder().mkdirs();
		saveDefaultConfig();
	}

	@Override
	public void onDisable() {
		getLogger().info(" ");
		getLogger().info(">>>>>>>>>>>>>>>>>>>>>>>> AntiModifySpawner Unloaded <<<<<<<<<<<<<<<<<<<<<<<<");
		getLogger().info(" ");
	}

	@Override
	public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
		if (sender instanceof Player && !sender.isOp()) {
			sender.sendMessage((prefix + config.get("only-op")).replace("&", "§"));
			return true;
		}
		if (args.length == 0 || args[0].equalsIgnoreCase("help")) {
			sendHelp(sender);
			return true;
		}
		if ("reload".equalsIgnoreCase(args[0])) {
			reloadConfig();
			config = getConfig();
			prefix = "&7[&b" + config.get("prompt-prefix") + "&7] &3: &r";
			sender.sendMessage((prefix + config.get("reload-success")).replace("&", "§"));
		} else if ("add".equalsIgnoreCase(args[0]) && args.length > 1) {
			String upperType = args[1].toUpperCase();
			List<String> allList = Arrays.asList(allTypeArray);
			if (!allList.contains(upperType)) {
				sender.sendMessage((prefix + config.get("invalid-type")).replace("&", "§"));
				return true;
			}
			List<String> currentList = config.getStringList("allow-type");
			if (currentList.contains(upperType)) {
				sender.sendMessage((prefix + config.get("already-set")).replace("&", "§"));
				return true;
			}
			currentList.add(upperType);
			config.set("allow-type", currentList);
			saveConfig();
			sender.sendMessage((prefix + config.get("add-success")).replace("&", "§"));
			sender.sendMessage((prefix + config.get("current-list") + "： " + StringUtils.join(currentList.toArray(new String[0]), ", ")).replace("&", "§"));
		} else if ("remove".equalsIgnoreCase(args[0]) && args.length > 1) {
			String upperType = args[1].toUpperCase();
			List<String> allList = Arrays.asList(allTypeArray);
			if (!allList.contains(upperType)) {
				sender.sendMessage((prefix + config.get("invalid-type")).replace("&", "§"));
				return true;
			}
			List<String> currentList = config.getStringList("allow-type");
			if (!currentList.contains(upperType)) {
				sender.sendMessage((prefix + config.get("not-set")).replace("&", "§"));
				return true;
			}
			List<String> resultList = currentList.stream().filter(item -> !item.equals(upperType)).collect(Collectors.toList());
			config.set("allow-type", resultList);
			saveConfig();
			sender.sendMessage((prefix + config.get("remove-success")).replace("&", "§"));
			if (resultList.isEmpty()) sender.sendMessage((prefix + config.get("empty-list")).replace("&", "§"));
			else sender.sendMessage((prefix + config.get("current-list") + "： " + StringUtils.join(resultList.toArray(new String[0]), ", ")).replace("&", "§"));
		} else if ("list".equalsIgnoreCase(args[0])) {
			List<String> list = config.getStringList("allow-type");
			if (list.isEmpty()) sender.sendMessage((prefix + config.get("empty-list")).replace("&", "§"));
			else sender.sendMessage((prefix + config.get("current-list") + "： " + StringUtils.join(list.toArray(new String[0]), ", ")).replace("&", "§"));
		} else if ("enum".equalsIgnoreCase(args[0])) {
			sender.sendMessage((prefix + config.get("enumerate-all") + "： " + StringUtils.join(allTypeArray, ", ")).replace("&", "§"));
		} else sendHelp(sender);
		return true;
	}

	private void sendHelp(CommandSender sender) {
		sender.sendMessage(("&m&a---&m&6--------&3 : " + config.get("prompt-prefix") + "&3 : &m&a--------&m&6---").replace("&", "§"));

		TextComponent addHelp = new TextComponent("/acs add <type>  " + config.get("add-help"));
		addHelp.setColor(ChatColor.GRAY);
		addHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/acs add "));
		addHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder((String) config.get("add-help")).color(ChatColor.BLUE).create()));
		sender.spigot().sendMessage(addHelp);

		TextComponent removeHelp = new TextComponent("/acs remove <type>  " + config.get("remove-help"));
		removeHelp.setColor(ChatColor.GRAY);
		removeHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/acs remove "));
		removeHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder((String) config.get("remove-help")).color(ChatColor.BLUE).create()));
		sender.spigot().sendMessage(removeHelp);

		TextComponent listHelp = new TextComponent("/acs list  " + config.get("list-help"));
		listHelp.setColor(ChatColor.GRAY);
		listHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/acs list"));
		listHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder((String) config.get("list-help")).color(ChatColor.BLUE).create()));
		sender.spigot().sendMessage(listHelp);

		TextComponent enumHelp = new TextComponent("/acs enum  " + config.get("enum-help"));
		enumHelp.setColor(ChatColor.GRAY);
		enumHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/acs enum"));
		enumHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder((String) config.get("enum-help")).color(ChatColor.BLUE).create()));
		sender.spigot().sendMessage(enumHelp);

		TextComponent reloadHelp = new TextComponent("/acs reload  " + config.get("reload-help"));
		reloadHelp.setColor(ChatColor.GRAY);
		reloadHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/acs reload"));
		reloadHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder((String) config.get("reload-help")).color(ChatColor.BLUE).create()));
		sender.spigot().sendMessage(reloadHelp);
	}

	public enum EggType {
		BAT,
		BLAZE,
		CAVE_SPIDER,
		CHICKEN,
		COD,
		COW,
		CREEPER,
		DOLPHIN,
		DONKEY,
		DROWNED,
		ELDER_GUARDIAN,
		ENDERMAN,
		ENDERMITE,
		EVOKER,
		GHAST,
		GUARDIAN,
		HORSE,
		HUSK,
		LLAMA,
		MAGMA_CUBE,
		MOOSHROOM,
		MULE,
		OCELOT,
		PARROT,
		PHANTOM,
		PIG,
		POLAR_BEAR,
		PUFFERFISH,
		RABBIT,
		SALMON,
		SHEEP,
		SHULKER,
		SILVERFISH,
		SKELETON_HORSE,
		SKELETON,
		SLIME,
		SPIDER,
		SQUID,
		STRAY,
		TROPICAL_FISH,
		TURTLE,
		VEX,
		VILLAGER,
		VINDICATOR,
		WITCH,
		WITHER_SKELETON,
		WOLF,
		ZOMBIE_HORSE,
		ZOMBIE_PIGMAN,
		ZOMBIE,
		ZOMBIE_VILLAGER,
	}

}